package com.maple.common.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.IntentFilter
import android.os.Build
import android.os.Handler

/**
 * Tip:
 * Create by SeVen on 2024/4/8 16:29
 */
object ReceiverUtils {
    fun registerReceiver(
        context: Context,
        receiver: BroadcastReceiver?,
        filter: IntentFilter?,
        broadcastPermission: String?,
        scheduler: Handler?
    ) {
        if (Build.VERSION.SDK_INT >= 33) {
            context.applicationContext.registerReceiver(
                receiver,
                filter,
                broadcastPermission,
                scheduler,
                Context.RECEIVER_EXPORTED
            )
        } else {
            context.applicationContext.registerReceiver(
                receiver,
                filter,
                broadcastPermission,
                scheduler
            )
        }
    }

    fun registerReceiver(context: Context, receiver: BroadcastReceiver?, filter: IntentFilter?) {
        if (Build.VERSION.SDK_INT >= 33) {
            context.applicationContext.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED)
        } else {
            context.applicationContext.registerReceiver(receiver, filter)
        }
    }

    fun unregisterReceiver(context: Context, receiver: BroadcastReceiver?) {
        context.applicationContext.unregisterReceiver(receiver)
    }
}
